package pATT.DataSetsGenerator.dataSetsGenerator.generator.visual;

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import pATT.DataSetsGenerator.dataSetsGenerator.attribute.implement.Attribute;
import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.RelationManager;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.DataManager;

@SuppressWarnings("serial")
public abstract class RandomDistributionFrame extends JFrame {
	
	protected String nameGenerator = "";
	protected DataManager manager = null;
	
	
	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JLabel jLabel = null;
	protected JLabel jLabel1 = null;
	protected JPanel jPanel = null;
	private JLabel jLabel2 = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private int contTable = 0;
	protected JLabel jErrorLabel1 = null;
	private JButton jHelpButton = null;
	
	public RandomDistributionFrame() throws HeadlessException {
		super();
		// TODO Auto-generated constructor stub
		initialize();
	}
	
	public RandomDistributionFrame(GraphicsConfiguration arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}
	
	public RandomDistributionFrame(String arg0) throws HeadlessException {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}
	
	public RandomDistributionFrame(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 330);
		this.setResizable(false);
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension freim = this.getSize();
		if (pantalla.width < freim.width){
			freim.width = pantalla.width;
		}
		if (pantalla.height < freim.height) {
			freim.height = pantalla.height;
		}
		this.setLocation((pantalla.width-freim.width)/2,(pantalla.height-freim.height)/2);

		this.setContentPane(getJContentPane());
		this.setTitle("Relation Generator Rule - "+nameGenerator);
		
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new java.awt.Rectangle(75,2,149,20));
			jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 13));
			jLabel1.setBackground(java.awt.SystemColor.control);
			jLabel1.setForeground(java.awt.Color.blue);
			jLabel1.setText("<Relation>");
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(4,2,72,20));
			jLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
			jLabel.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 13));
			jLabel.setBackground(java.awt.SystemColor.control);
			jLabel.setForeground(java.awt.Color.blue);
			jLabel.setText("Relation: ");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(java.awt.SystemColor.control);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJPanel(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(getJHelpButton(), null);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new java.awt.Rectangle(4,34,221,221));
			jScrollPane.setViewportView(getJTable());
			
		}
		return jScrollPane;
	}
	
	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			TableColumn ivjTableColumn2 = new TableColumn();
			TableColumn ivjTableColumn1 = new TableColumn();
			TableColumn ivjTableColumn = new TableColumn();
			jTable = new JTable();
			jTable.setAutoCreateColumnsFromModel(false);
			ivjTableColumn.setHeaderValue("NAME");
			ivjTableColumn2.setHeaderValue("TYPE");
			ivjTableColumn1.setHeaderValue("NULL");
			ivjTableColumn.setModelIndex(0);
			ivjTableColumn2.setModelIndex(1);
			ivjTableColumn1.setModelIndex(2);
			jTable.setModel(new ToDoListModel());
			
			jTable.addColumn(ivjTableColumn);
			jTable.addColumn(ivjTableColumn2);
			jTable.addColumn(ivjTableColumn1);
			loadTable();
			
		}
		return jTable;
	}
	
	@SuppressWarnings("unchecked")
	public void loadTable(){
		Vector vec=new Vector();
		vec.addAll(RelationManager.instance.getCurrentRelation().getAtributes());
		for (Enumeration e=vec.elements();e.hasMoreElements();){
			Attribute attr=(Attribute) e.nextElement();
			TableModel model = jTable.getModel();
			model.setValueAt(attr.getName(),contTable,0);
			model.setValueAt(attr.getType(),contTable, 1);
			model.setValueAt(String.valueOf(attr.isNull()),contTable, 2);
			contTable++;
		}
		
	}
	
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new java.awt.Rectangle(23,10,186,24));
			jLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jLabel2.setText("Specify generation options for attribute");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new java.awt.Rectangle(241,34,347,219));
			jPanel.setBackground(java.awt.SystemColor.activeCaptionBorder);
			jPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
			jPanel.add(jLabel2, null);
			
			jErrorLabel1 = new JLabel();
			jErrorLabel1.setText("Please, complete all attributes");
			jErrorLabel1.setLocation(new java.awt.Point(15,190));
			jErrorLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jErrorLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.BOLD, 11));
			jErrorLabel1.setForeground(java.awt.Color.red);
			jErrorLabel1.setSize(new java.awt.Dimension(321,23));
			jErrorLabel1.setVisible(false);
			jPanel.add(jErrorLabel1, null);
			
			
		}
		return jPanel;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new java.awt.Rectangle(343,263,69,20));
			jButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton.setBackground(java.awt.SystemColor.control);
			jButton.setText("Generate");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					manager.random();
					dispose();
				}
			});
		}
		return jButton;
	}
	
	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Cancel");
			jButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jButton1.setSize(new java.awt.Dimension(69,20));
			jButton1.setFont(new java.awt.Font("Microsoft Sans Serif", java.awt.Font.PLAIN, 11));
			jButton1.setBackground(java.awt.SystemColor.control);
			jButton1.setLocation(new java.awt.Point(420,263));
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jButton1;
	}
	
	/**
	 * Static inner class for the table model
	 */
	@SuppressWarnings("serial")
	public static class ToDoListModel extends AbstractTableModel {
		
		String[][] data = { { "", "", "" }, { "", "" , ""}, { "", "", "" }, { "", "", "" },
				{ "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""},
				{ "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""},
				{ "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""},
				{ "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""},
				{ "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""}, { "", "" , ""},
				{ "", "" , ""}, { "", "" , ""}, { "", "" , ""} };
		
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex];
		}
		
		public void cleanTable(int pos) {
			System.out.println("pos: "+pos);
			
			for (int j = 0; j <= pos; j++) {
				setValueAt("",j,0);
				setValueAt("",j,1);
				setValueAt("",j,2);
			}
		}
		
		public int getColumnCount() {
			return 0;
		}
		
		public int getRowCount() {
			return data.length;
		}
		
		public String getColumnName(int c) {
			return ("");
		}
		
		public void setValueAt(Object arg0, int arg1, int arg2) {
			data[arg1][arg2] = (String) arg0;
			fireTableDataChanged();
		}
	}

	/**
	 * This method initializes jHelpButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJHelpButton() {
		if (jHelpButton == null) {
			jHelpButton = new JButton();
			jHelpButton.setText("Help");
			jHelpButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jHelpButton.setSize(new java.awt.Dimension(55,35));
			jHelpButton.setLocation(new java.awt.Point(532,259));
			jHelpButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					callHelp();
				}
			});
		}
		return jHelpButton;
	}

	protected abstract void callHelp();
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
